<?php

namespace common\models\datas;

use common\components\extenders\MenuManage;
use common\models\datas\Customer;
use EasyWeChat\Support\Arr;
use frontend\models\datas\Orders;
use frontend\models\datas\OrdersItems;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%deposit_log}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property integer $orders_items_id
 * @property string $money
 * @property integer $pay_type
 * @property integer $created_at
 * @property integer $status
 * @property string $code
 * @property string $transaction_id
 * @property string $actual_money
 * @property integer $operator_id
 * @property string $operator_name
 * @property integer $application_time
 * @property integer $processing_time
 * @property string $remarks
 * @property integer $processing_status
 */
class DepositLog extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%deposit_log}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['customer_id', 'orders_id', 'orders_items_id', 'pay_type', 'created_at', 'status', 'operator_id', 'application_time', 'processing_time', 'processing_status','student_id'], 'integer'],
            [['money', 'actual_money', 'site_id', 'order_cash_money'], 'number'],
            [['remarks'], 'string'],
            [['code', 'refund_no'], 'string', 'max' => 100],
            [['transaction_id'], 'string', 'max' => 200],
            [['operator_name'], 'string', 'max' => 255],
            [['actual_money'], 'check_actual_money', 'on' => 'handle'],
            [['processing_status'], 'required', 'on' => 'handle']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自增id'),
            'customer_id' => Yii::t('common', '持有奖学金会员 id'),
            'orders_id' => Yii::t('common', '订单 id'),
            'orders_items_id' => Yii::t('common', '订单课程id'),
            'money' => Yii::t('common', '缴纳保证金金额'),
            'pay_type' => Yii::t('common', '支付方式'),
            'created_at' => Yii::t('common', '支付时间'),
            'status' => Yii::t('common', '申请退款'),
            'code' => Yii::t('common', '交纳保证金的唯一单号'),
            'transaction_id' => Yii::t('common', '线上支付返回的订单号'),
            'actual_money' => Yii::t('common', '实际退款金额'),
            'operator_id' => Yii::t('common', '操作人id'),
            'operator_name' => Yii::t('common', '操作人名字'),
            'application_time' => Yii::t('common', '申请退款时间'),
            'processing_time' => Yii::t('common', '处理时间'),
            'remarks' => Yii::t('common', '处理备注'),
            'processing_status' => Yii::t('common', '处理状态'),
            'order_cash_money' => Yii::t('common', '微信支付总金额'),
            'refund_no' => Yii::t('common', '退款后生成单号'),
            'student_id' => Yii::t('common', '学生id'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['application_time'] = function () {
            if ($this->application_time > 0)
                return \Yii::$app->formatter->asDatetime($this->application_time);
            else
                return Yii::t('common', '未申请');
        };
        $fields['processing_time'] = function () {
            if ($this->processing_time > 0) {
                if ($this->processing_time == 1) {
                    return Yii::t('common', '已申请');
                } else {
                    return \Yii::$app->formatter->asDatetime($this->processing_time);
                }
            } else {
                return Yii::t('common', '未申请');
            }
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'pay_type_name' => function () {
                return isset($this->payment) ? $this->payment->name : '';
            },
            'status_name' => function () {
                switch ($this->status) {
                    case 0:
                        return Yii::t('common', '否');
                    case 1:
                        return \Yii::t('common', '是');
                    case 2:
                        return \Yii::t('common', '已转学费');
                }
            },
            'processing_name' => function () {
                switch ($this->processing_status) {
                    case 0:
                        return Yii::t('common', '正常');
                    case 1:
                        return \Yii::t('common', '申请中');
                    case 2:
                        return \Yii::t('common', '审核中');
                    case 3:
                        return \Yii::t('common', '通过');
                    case 4:
                        return \Yii::t('common', '失败');
                    case 5:
                        return \Yii::t('common', '转学费');
                }
            },
            'customer_name' => function () {
                return isset($this->customer) ? $this->customer->name : '';
            },
            'items_name' => function () {
                return isset($this->ordersItem) ? $this->ordersItem->name : '';
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                        'apply' => function ($data) {
                            if ($data['status'] == 1 || $data['status'] == 2) {
                                return '<button class="yx-btn-disabled" type="button">申请退款</button>';
                            } else {
                                return '';
                            }
                        },
                        'handle' => function ($data) {
                            if ($data['processing_status'] == 2 || $data['processing_status'] == 3 || $data['processing_status'] == 4 || $data['processing_status'] == 5 || $data['status'] == 0) {
                                return '<button class="yx-btn-disabled" type="button">处理申请</button>';
                            } else {
                                return '';
                            }
                        },
                        'classdetails' => function ($data) {
                            if ($data['processing_status'] == 0) {
                                return '<button class="yx-btn-disabled">查看课次</button>';
                            } else {
                                return '';
                            }
                        },

                    ]
                )->create();
            },
        ];
    }

    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }

    public function getOrders()
    {
        return $this->hasOne(Orders::className(), ['id' => 'orders_id']);
    }

    public function getOrdersItem()
    {
        return $this->hasOne(OrdersItems::className(), ['id' => 'orders_items_id']);
    }

    public function getPayment()
    {
        return $this->hasOne(Payment::className(), ['id' => 'pay_type']);
    }

    public function check_actual_money($attribute)
    {
        if ($this->$attribute > $this->money) {
            $this->addError($attribute, Yii::t('frontend', '输入的金额大于实付保证金金额'));
        }
        if ($this->processing_status == 3 && $this->$attribute <= 0) {
            $this->addError($attribute, Yii::t('frontend', '退款金额大于0'));
        }
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }
}
